package com.haisen.controller;


import com.alibaba.fastjson.JSON;
import com.haisen.core.annotation.SystemLog;
import com.haisen.core.utils.PageResultSet;
import com.haisen.core.utils.Result;
import com.haisen.dataobject.entity.Income;
import com.haisen.dataobject.query.IncomeQuery;
import com.haisen.service.AreaService;
import com.haisen.service.IncomeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

@Controller
@RequestMapping("/finance/income")
@Slf4j
public class IncomeController {

    @Autowired
    private IncomeService incomeService;

    @Autowired
    private AreaService areaService;


    @GetMapping
    @RequiresPermissions("income:view")
    public String page(Model model) {
        setCommonData(model);
        return "finance/income";
    }

    @ResponseBody
    @RequiresPermissions("income:create")
    @SystemLog("财务管理创建收入")
    @PostMapping("/create")
    public Result create(@Valid Income income) {
        incomeService.create(income);
        return Result.success();
    }

    @ResponseBody
    @RequiresPermissions("income:update")
    @SystemLog("财务管理修改收入")
    @PostMapping("/upate")
    public Result update(@Valid Income income) {
        incomeService.update(income);
        return Result.success();
    }


    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("income:view")
    public PageResultSet<Income> list(IncomeQuery incomeQuery) {
        log.info("查询对象条件：{}", JSON.toJSONString(incomeQuery));
        return incomeService.findByPage(incomeQuery);
    }

    private void setCommonData(Model model){
        //查一级地市
        model.addAttribute("areaList",areaService.getCityCountry(Long.valueOf(1)));

    }

}
